package com.wf.ew.sell.biz.impl;

import com.wf.ew.common.BaseDao;
import com.wf.ew.common.PageResult;
import com.wf.ew.common.utils.Utils;
import com.wf.ew.sell.model.EntryBillSon;
import com.wf.ew.sell.model.Sell;
import com.wf.ew.system.model.User;
import com.wf.ew.common.constant.UserConstant;
import com.wf.ew.sell.biz.EntryBillBiz;
import com.wf.ew.sell.model.EntryBill;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2019-11-24 00:07:19
 * @version ：1.0
 */
@Repository
public class EntryBillBizImpl extends BaseDao implements EntryBillBiz {

	@Override
	public String list(PageResult<T> page, EntryBill entryBill,
					   EntryBillSon entryBillSon, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EntryBill> rm = new BeanPropertyRowMapper<EntryBill>(EntryBill.class);

		StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.code");
        sql.append(",e.supplier,e.entryTime,e.warehouseName,e.remark");
        sql.append(",e.state,e.invoice,e.total,e.createTime");
		sql.append(",e.createUser,e.updateTime,e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
		if(Utils.isNotEmpty(entryBillSon)) {
			if (Utils.isNotEmpty(entryBillSon.getSpecification())) {
				sql.append(" left join we_entry_bill_son es on es.entryBillId=e.id ");
			}
		}
        sql.append(" where e.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and e.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }

		if(Utils.isNotEmpty(entryBillSon)) {
			if (Utils.isNotEmpty(entryBillSon.getSpecification())) {
				sql.append(" and instr(es.specification,:specification)>0 ");
				map.put("specification", entryBillSon.getSpecification());
			}
		}
    	if(Utils.isNotEmpty(entryBill)){
			if(Utils.isNotEmpty(entryBill.getKeywords())){
				sql.append(" and (instr(e.code,:code)>0 or instr(e.supplier,:code)>0)");
				map.put("code", entryBill.getKeywords());
			}
			if(Utils.isNotEmpty(entryBill.getDate1())){
				entryBill.setDate1(entryBill.getDate1()+" 00:00:00");
				sql.append(" and e.entryTime>=DATE_FORMAT(:date1,'%Y-%m-%d %H:%i:%s') ");
				map.put("date1", entryBill.getDate1());
			}
			if(Utils.isNotEmpty(entryBill.getDate2())){
				entryBill.setDate2(entryBill.getDate2()+" 23:59:59");
				sql.append(" and e.entryTime<=DATE_FORMAT(:date2,'%Y-%m-%d %H:%i:%s') ");
				map.put("date2", entryBill.getDate2());
			}
			if(Utils.isNotEmpty(entryBill.getState())){
				sql.append(" and e.state=:state ");
				map.put("state", entryBill.getState());
			}
			if(Utils.isNotEmpty(entryBill.getInvoice())){
				sql.append(" and e.invoice=:invoice ");
				map.put("invoice", entryBill.getInvoice());
			}
			if(Utils.isNotEmpty(entryBill.getSort())&&Utils.isNotEmpty(entryBill.getOrder())){
				sql.append(" order by "+entryBill.getSort()+" "+entryBill.getOrder());
			} else {
				sql.append(" order by e.entryTime desc ");
			}
		}
		return jsonPage(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<EntryBill> list(EntryBill entryBill, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<EntryBill> rm = new BeanPropertyRowMapper<EntryBill>(EntryBill.class);

        StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.code");
        sql.append(",e.supplier,e.entryTime,e.warehouseName,e.remark");
		sql.append(",e.state,e.invoice,e.total,e.createTime");
		sql.append(",e.createUser,e.updateTime,e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
        sql.append(" where e.del=0 and cmy.del=0 ");
		if(!loginUser.getId().equals(UserConstant.ADMIN_ID)){
            sql.append(" and e.companyId=:tablecompanyId ");
            map.put("tablecompanyId", loginUser.getCompanyId());
        }
    	if(Utils.isNotEmpty(entryBill)){
			if(Utils.isNotEmpty(entryBill.getKeywords())){
				sql.append(" and (instr(e.code,:code)>0 or instr(e.supplier,:code)>0) ");
				map.put("code", entryBill.getKeywords());
			}
			if(Utils.isNotEmpty(entryBill.getDate1())){
				entryBill.setDate1(entryBill.getDate1()+" 00:00:00");
				sql.append(" and e.entryTime>=DATE_FORMAT(:date1,'%Y-%m-%d %H:%i:%s') ");
				map.put("date1", entryBill.getDate1());
			}
			if(Utils.isNotEmpty(entryBill.getDate2())){
				entryBill.setDate2(entryBill.getDate2()+" 23:59:59");
				sql.append(" and e.entryTime<=DATE_FORMAT(:date2,'%Y-%m-%d %H:%i:%s') ");
				map.put("date2", entryBill.getDate2());
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public EntryBill getById(EntryBill entryBill) {
		RowMapper<EntryBill> rm = new BeanPropertyRowMapper<EntryBill>(EntryBill.class);
		Map<String, String> map  = new HashMap<String, String>();

		StringBuffer sql = new StringBuffer("select e.id,e.companyId,e.code");
        sql.append(",e.supplier,e.entryTime,e.warehouseName,e.remark");
		sql.append(",e.state,e.invoice,e.total,e.createTime");
		sql.append(",e.createUser,e.updateTime,e.updateUser");
        sql.append(",cmy.name companyName,suser.name createName ");
        sql.append(" from we_entry_bill e ");
        sql.append(" inner join sys_user suser on suser.id=e.createUser ");
        sql.append(" inner join we_company cmy on cmy.id=e.companyId ");
        sql.append(" where e.del=0 and cmy.del=0 and e.id=:id ");
		map.put("id", entryBill.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public KeyHolder add(EntryBill entryBill, User loginUser) {
		int count = 1;
		KeyHolder keyHolder=new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into we_entry_bill (");
        sql.append("companyId,code,supplier,entryTime");
		sql.append(",warehouseName,remark,state,invoice,total,createUser) values ( ");
        sql.append(":companyId,:code,:supplier,:entryTime");
		sql.append(",:warehouseName,:remark,:state,:invoice,:total,:createUser)");
        map.put("companyId", loginUser.getCompanyId());
        map.put("code", entryBill.getCode());
        map.put("supplier", entryBill.getSupplier());
        map.put("entryTime", entryBill.getEntryTime());
        map.put("warehouseName", entryBill.getWarehouseName());
        map.put("remark", entryBill.getRemark());
        map.put("state", entryBill.getState());
        map.put("invoice", entryBill.getInvoice());
        map.put("total", entryBill.getTotal()+"");
		map.put("createUser", loginUser.getId());
		getMapParameter(sql, map);
		entryBill.setCompanyId(loginUser.getCompanyId());
		entryBill.setCreateUser(Integer.valueOf(loginUser.getId()));
		SqlParameterSource paramSource=new BeanPropertySqlParameterSource(entryBill);
		count = jdbcTemplate.update(sql.toString(), paramSource,keyHolder,new String[]{"id"});
		return keyHolder;
	}

	@Override
	public boolean update(EntryBill entryBill, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_entry_bill set ");
        sql.append(" supplier=:supplier,entryTime=:entryTime ");
		sql.append(",remark=:remark,state=:state,invoice=:invoice," +
				"total=:total,warehouseName=:warehouseName," +
				"updateTime=now(),updateUser=:updateUser where id=:id ");
        map.put("supplier", entryBill.getSupplier());
        map.put("entryTime", entryBill.getEntryTime());
        map.put("remark", entryBill.getRemark());
        map.put("state", entryBill.getState());
        map.put("invoice", entryBill.getInvoice());
        map.put("total", entryBill.getTotal()+"");
        map.put("warehouseName", entryBill.getWarehouseName());
		map.put("updateUser", loginUser.getId());
		map.put("id", entryBill.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateState(EntryBill entryBill, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_entry_bill set ");
		if(Utils.isNotEmpty(entryBill.getState())){
			sql.append(" state=:state, ");
		}
		if(Utils.isNotEmpty(entryBill.getInvoice())){
			sql.append(" invoice=:invoice, ");
		}
		sql.append("updateTime=now(),updateUser=:updateUser where id=:id ");
		if(Utils.isNotEmpty(entryBill.getState())){
			map.put("state", entryBill.getState());
		}
		if(Utils.isNotEmpty(entryBill.getInvoice())){
			map.put("invoice", entryBill.getInvoice());
		}
		map.put("updateUser", loginUser.getId());
		map.put("id", entryBill.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean updateTotal(EntryBill entryBill, User loginUser) {
		int count = 0;
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update we_entry_bill set ");
		sql.append(" total=:total ");
		sql.append(",updateTime=now(),updateUser=:updateUser where id=:id ");
		map.put("total", entryBill.getTotal()+"");
		map.put("updateUser", loginUser.getId());
		map.put("id", entryBill.getId());
		getMapParameter(sql, map);
		count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean delete(EntryBill entryBill, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(entryBill.getIds().length()>0){
			for(String ID : entryBill.getIds().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update we_entry_bill set del=1" +
						",updateUser=:updateUser, updateTime=now() " 
						+ " where id =:id ");
				map.put("updateUser", loginUser.getId());
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
